package com.ruoyi.system.controller;

import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.vo.SysOperLogVo;
import com.ruoyi.system.domain.bo.SysOperLogBo;
import com.ruoyi.system.service.ISysOperLogService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 操作日志记录
 *
 * @author ruoyi
 * @date 2023-11-27
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/operLog")
public class SysOperLogController extends BaseController {

    private final ISysOperLogService iSysOperLogService;

    /**
     * 查询操作日志记录列表
     */
    @SaCheckPermission("system:operLog:list")
    @GetMapping("/list")
    public TableDataInfo<SysOperLogVo> list(SysOperLogBo bo, PageQuery pageQuery) {
        return iSysOperLogService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出操作日志记录列表
     */
    @SaCheckPermission("system:operLog:export")
    @Log(title = "操作日志记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SysOperLogBo bo, HttpServletResponse response) {
        List<SysOperLogVo> list = iSysOperLogService.queryList(bo);
        ExcelUtil.exportExcel(list, "操作日志记录", SysOperLogVo.class, response);
    }

    /**
     * 获取操作日志记录详细信息
     *
     * @param operId 主键
     */
    @SaCheckPermission("system:operLog:query")
    @GetMapping("/{operId}")
    public R<SysOperLogVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long operId) {
        return R.ok(iSysOperLogService.queryById(operId));
    }

    /**
     * 新增操作日志记录
     */
    @SaCheckPermission("system:operLog:add")
    @Log(title = "操作日志记录", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SysOperLogBo bo) {
        return toAjax(iSysOperLogService.insertByBo(bo));
    }

    /**
     * 修改操作日志记录
     */
    @SaCheckPermission("system:operLog:edit")
    @Log(title = "操作日志记录", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SysOperLogBo bo) {
        return toAjax(iSysOperLogService.updateByBo(bo));
    }

    /**
     * 删除操作日志记录
     *
     * @param operIds 主键串
     */
    @SaCheckPermission("system:operLog:remove")
    @Log(title = "操作日志记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{operIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] operIds) {
        return toAjax(iSysOperLogService.deleteWithValidByIds(Arrays.asList(operIds), true));
    }
}
